package com.cnbm.wcsbasic.protocol.netty.modbus.entity;


import com.cnbm.wcsbasic.protocol.netty.io.common.byteutils.ByteUtils;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2024-04-25 10:15
 */
public class ResponseRes {
    private boolean isOK;
    private String content;
    private Object value;

    public ResponseRes(byte fc) {
        isValidOfFunctioncode(fc);
    }

    public ResponseRes(boolean isOK, String content, Object value) {
        this.isOK = isOK;
        this.content = content;
        this.value = value;
    }

    public void isValidOfFunctioncode(Byte code){
        if(ByteUtils.getByteSignbit(code, false)){
            //负数
            this.isOK = false;
            if(code.equals(Byte.valueOf("-127"))){

                this.content = "非法功能";

            }else if(code.equals(Byte.valueOf("-126"))){

                this.content = "非法数据地址";

            }else if(code.equals(Byte.valueOf("-125"))){

                this.content = "非法数据值";

            }else if(code.equals(Byte.valueOf("-124"))){

                this.content = "从站设备故障";

            }else if(code.equals(Byte.valueOf("-123"))){

                this.content = "确认";

            }else if(code.equals(Byte.valueOf("-122"))){

                this.content = "服务器繁忙";

            }else if(code.equals(Byte.valueOf("-121"))){

                this.content = "存储奇偶性差错";

            }else if(code.equals(Byte.valueOf("-118"))){

                this.content = "不可用网关路径";

            }else if(code.equals(Byte.valueOf("-117"))){

                this.content = "网关目标设备响应失败";

            }else {

                this.content = "其他错误";

            }
        }else {

            this.isOK = true;
            this.content = "没错误";
        }

    }

    public boolean isOK() {
        return isOK;
    }

    public String getContent() {
        return content;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "ResponseRes{" +
                "isOK=" + isOK +
                ", content='" + content + '\'' +
                ", value=" + value +
                '}';
    }
}
